#include <SDL/SDL.h>
#include <stdio.h>
#include <SDL_mixer.h>

int collision(SDL_Rect* rect1,SDL_Rect* rect2)
{
        if(rect1->y >= rect2->y + rect2->h)
                return 0;
        if(rect1->x >= rect2->x + rect2->w)
                return 0;
        if(rect1->y + rect1->h <= rect2->y)
                return 0;
        if(rect1->x + rect1->w <= rect2->x)
                return 0;
        return 1;
}

int niveau1(SDL_Surface* screen)
{
    SDL_Surface *terre,*ted,*bon1,*bon2,*bon3,*bon4,*bon5;
    SDL_Rect posTed,posBon1,posBon2,posBon3,posBon4,posBon5;
    SDL_Event event;
    if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 1024) == -1)
    {
        printf("%s", Mix_GetError());
    }
    Mix_Music *musique;
    musique = Mix_LoadMUS("facile/son1.mp3");
    Mix_PlayMusic(musique, -1);
    int run=1,direction=1,bonbon=0,i;

    posTed.x=32;
    posTed.y=85;

    posBon1.x=107;
    posBon1.y=167;

    posBon2.x=200;
    posBon2.y=85;

    posBon3.x=375;
    posBon3.y=85;

    posBon4.x=29;
    posBon4.y=434;

    posBon5.x=375;
    posBon5.y=511;

	terre=SDL_LoadBMP("facile/Niveau1.bmp");
	if (terre == NULL) {
		printf("Unable to load bitmap: %s\n", SDL_GetError());
		return 1;
	}
	bon1=SDL_LoadBMP("facile/bonbon.bmp");
	if (bon1 == NULL) {
		printf("Unable to load bitmap: %s\n", SDL_GetError());
		return 1;
	}
	bon2=SDL_LoadBMP("facile/bonbon.bmp");
	bon3=SDL_LoadBMP("facile/bonbon.bmp");
	bon4=SDL_LoadBMP("facile/bonbon.bmp");
	bon5=SDL_LoadBMP("facile/bonbon.bmp");
	ted=SDL_LoadBMP("facile/ted_right.bmp");
	SDL_SetColorKey(bon1, SDL_SRCCOLORKEY, SDL_MapRGB(bon1->format, 255, 255, 255));
	SDL_SetColorKey(bon2, SDL_SRCCOLORKEY, SDL_MapRGB(bon2->format, 255, 255, 255));
	SDL_SetColorKey(bon3, SDL_SRCCOLORKEY, SDL_MapRGB(bon3->format, 255, 255, 255));
	SDL_SetColorKey(bon4, SDL_SRCCOLORKEY, SDL_MapRGB(bon4->format, 255, 255, 255));
    SDL_SetColorKey(bon5, SDL_SRCCOLORKEY, SDL_MapRGB(bon5->format, 255, 255, 255));
	SDL_SetColorKey(ted, SDL_SRCCOLORKEY, SDL_MapRGB(ted->format, 255, 255, 255));
	if (ted == NULL) {
		printf("Unable to load bitmap: %s\n", SDL_GetError());
		return 1;
	}
	SDL_BlitSurface(terre,NULL,screen,NULL);
	SDL_BlitSurface(ted,NULL,screen,&posTed);

	SDL_Flip(screen);
	        SDL_Rect mur[15];
        mur[0].x=13;
        mur[0].y=74;
        mur[0].w=10;
        mur[0].h=589;

        mur[1].x=12;
        mur[1].y=67;
        mur[1].w=447;
        mur[1].h=10;

        mur[2].x=13;
        mur[2].y=154;
        mur[2].h=10;
        mur[2].w=80;

        mur[3].x=448;
        mur[3].y=67;
        mur[3].h=510;
        mur[3].w=10;

        mur[4].x=115;
        mur[4].y=580;
        mur[4].h=1;
        mur[4].w=344;

        mur[5].x=183;
        mur[5].y=79;
        mur[5].w=10;
        mur[5].h=330;

        mur[6].x=196;
        mur[6].y=152;
        mur[6].w=80;
        mur[6].h=10;

        mur[7].x=104;
        mur[7].y=243;
        mur[7].w=80;
        mur[7].h=10;

        mur[8].x=98;
        mur[8].y=359;
        mur[8].w=5;
        mur[8].h=136;

        mur[9].x=30;
        mur[9].y=496;
        mur[9].w=343;
        mur[9].h=10;

        mur[10].x=359;
        mur[10].y=79;
        mur[10].w=13;
        mur[10].h=172;

        mur[11].x=275;
        mur[11].y=242;
        mur[11].w=97;
        mur[11].h=9;

        mur[12].x=275;
        mur[12].y=242;
        mur[12].w=10;
        mur[12].h=167;

        mur[13].x=360;
        mur[13].y=348;
        mur[13].w=10;
        mur[13].h=159;

        mur[14].x=13;
        mur[14].y=660;
        mur[14].w=446;
        mur[14].h=5;

        Uint32 color = SDL_MapRGB(screen->format, 0,0,0);
        for(int i = 0; i < 15; i += 1) {
                        SDL_FillRect(screen,&mur[i],color);
        }
	SDL_EnableKeyRepeat(10,100);

	while(run!=0)
    {
        SDL_SetColorKey(ted, SDL_SRCCOLORKEY, SDL_MapRGB(ted->format, 255, 255, 255));

        SDL_BlitSurface(terre,NULL,screen,NULL);
        SDL_BlitSurface(bon1,NULL,screen,&posBon1);
        SDL_BlitSurface(bon2,NULL,screen,&posBon2);
        SDL_BlitSurface(bon3,NULL,screen,&posBon3);
        SDL_BlitSurface(bon4,NULL,screen,&posBon4);
        SDL_BlitSurface(bon5,NULL,screen,&posBon5);

        SDL_BlitSurface(ted,NULL,screen,&posTed);


        SDL_WaitEvent(&event);
        switch(event.type)
        {
        case SDL_KEYDOWN:
            switch(event.key.keysym.sym)
            {
            case SDLK_ESCAPE:
                run=0;
                break;
            case SDLK_UP:
                posTed.y-=6;
                for( i = 0; i < 15; i += 1)
                                if(collision(&posTed,&mur[i]))
                                        posTed.y+=6;
                ted= SDL_LoadBMP("facile/ted_right.bmp");
                break;
            case SDLK_DOWN:
                posTed.y+=6;
                for( i = 0; i < 15; i += 1)
                                if(collision(&posTed,&mur[i]))
                                        posTed.y-=6;
                ted= SDL_LoadBMP("facile/ted_right.bmp");
                break;
            case SDLK_RIGHT:
                posTed.x+=6;
                for( i = 0; i < 15; i += 1)
                                if(collision(&posTed,&mur[i]))
                                        posTed.x-=6;
                ted= SDL_LoadBMP("facile/ted_right.bmp");
                break;
            case SDLK_LEFT:
                posTed.x-=6;
                for( i = 0; i < 15; i += 1)
                                if(collision(&posTed,&mur[i]))
                                        posTed.x+=6;
                ted= SDL_LoadBMP("facile/ted_left.bmp");
                break;

            }
            break;
        }
        if((posTed.x>= 90 && posTed.x<= 130 )  && (posTed.y>= 135 && posTed.y<= 170 ))
            {
            bon1=NULL;
            }
        if((posTed.x>= 191 && posTed.x<= 231 )  && (posTed.y>= 63 && posTed.y<= 87 ))
            {
            bon2=NULL;
            }
        if((posTed.x>= 340 && posTed.x<= 380 )  && (posTed.y>= 75 && posTed.y<= 110 ))
            {
            bon3=NULL;
            }
        if((posTed.x>= 12 && posTed.x<= 52 )  && (posTed.y>= 401 && posTed.y<= 420 ))
            {
            bon4=NULL;
            }
        if((posTed.x>= 358 && posTed.x<= 400 )  && (posTed.y>= 478 && posTed.y<= 497 ))
            {
            bon5=NULL;
            }
        if(bon1==NULL)
            bonbon++;
        if(bon2==NULL)
            bonbon++;
        if(bon3==NULL)
            bonbon++;
        if(bon4==NULL)
            bonbon++;
        if(bon5==NULL)
            bonbon++;
        if ((posTed.x>=381 && posTed.x<=400) && (posTed.y>=580 && posTed.y<=655))
        {
            ;// Gagner
        }



        SDL_Flip(screen);
    }




    SDL_FreeSurface(terre);
    SDL_FreeSurface(ted);
    Mix_FreeMusic(musique);
    return bonbon;
}


int Confirm_nouveau(SDL_Surface* screen)
{


    SDL_Surface *fond=NULL;
    SDL_Rect pos;
    SDL_Event e;
    int cont=1, conteur2=1,j=1,score1=0;
    fond=SDL_LoadBMP("Menu/Facile.bmp");
    pos.x=0;
    pos.y=0;

    SDL_BlitSurface(fond, NULL , screen, &pos);
	SDL_Flip(screen);

	while (cont!=0)
        {

            switch(j)
        {
        case 1:
            fond= SDL_LoadBMP("Menu/Facile.bmp");
            SDL_BlitSurface(fond, NULL , screen, &pos);
            SDL_Flip(screen);
            SDL_FreeSurface(fond);
            break;

        case 2:
            fond= SDL_LoadBMP("Menu/Difficile.bmp");
            SDL_BlitSurface(fond, NULL , screen, &pos);
            SDL_Flip(screen);
            SDL_FreeSurface(fond);
            break;
        }

        conteur2=1;

        while (conteur2!=0)
        {

            SDL_WaitEvent(&e);

            switch(e.type)
            {
            case SDL_KEYDOWN:
                switch(e.key.keysym.sym)
                {
                case SDLK_UP:
                    switch(j)
                    {
                    case 1:
                        j=2;
                        break;
                    case 2:
                        j=1;
                        break;

                    }
                    break;
                case SDLK_DOWN:
                    switch(j)
                    {
                    case 1:
                        j=2;
                        break;
                    case 2:
                        j=1;
                        break;


                    }
                    break;
                case SDLK_RETURN:
                    switch(j)
                    {
                        case 1:
                            score1=niveau1(screen);
                        break;
                    case 2:
                            //Difficile
                        break;
                    }
                    break;
                }

                break;

            }
        conteur2=0;
        }
                    if (e.key.keysym.sym==SDLK_ESCAPE)
            {
                cont=0;
            }
        }
        return score1;
}


void Confirm_aide(SDL_Surface* screen)
{
    SDL_Surface *fond=NULL;
    SDL_Rect pos;
    SDL_Event e;
    int conteur=1;
    fond=SDL_LoadBMP("Menu/Aide.bmp");
    pos.x=0;
    pos.y=0;

    SDL_BlitSurface(fond, NULL , screen, &pos);
	SDL_Flip(screen);

	while (conteur!=0)
        {

            SDL_WaitEvent(&e);

            if (e.key.keysym.sym==SDLK_ESCAPE)
            {
                conteur=0;
            }
        }
        SDL_FreeSurface(fond);
}



int main(int argc, char *argv[])
{
    SDL_Surface *screen = NULL, *fond = NULL,*imgIcon=NULL;
    SDL_Rect posFond;
    SDL_Event event;
    int run=1, pause=1, i=1,SCORE;
    SDL_Init(SDL_INIT_VIDEO);
    SDL_putenv("SDL_VIDEO_CENTERED=center");
    screen= SDL_SetVideoMode(473, 668, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    if (screen == NULL) {
		printf("Unable to load bitmap: %s\n", SDL_GetError());
		return 1;
	}
	fond= SDL_LoadBMP("Menu/Nouveau jeu.bmp");
	if (fond == NULL) {
		printf("Unable to load bitmap: %s\n", SDL_GetError());
		return 1;
	}
	imgIcon= SDL_LoadBMP("Menu/miniTed.bmp");
	if (imgIcon == NULL) {
		printf("Unable to load bitmap: %s\n", SDL_GetError());
		return 1;
	}
	SDL_WM_SetIcon(imgIcon, NULL);
	SDL_WM_SetCaption("Mini Projet Labyrinthe",NULL);
	posFond.x=0;
	posFond.y=0;
	SDL_BlitSurface(fond, NULL , screen, &posFond);
	SDL_Flip(screen);
	SDL_FreeSurface(fond);
	while (run !=0)
    {
        switch(i)
        {
        case 1:
            fond= SDL_LoadBMP("Menu/Nouveau jeu.bmp");
            SDL_BlitSurface(fond, NULL , screen, &posFond);
            SDL_Flip(screen);
            SDL_FreeSurface(fond);
            break;

        case 2:
            fond= SDL_LoadBMP("Menu/Continuer.bmp");
            SDL_BlitSurface(fond, NULL , screen, &posFond);
            SDL_Flip(screen);
            SDL_FreeSurface(fond);
            break;

        case 3:
            fond= SDL_LoadBMP("Menu/Options.bmp");
            SDL_BlitSurface(fond, NULL , screen, &posFond);
            SDL_Flip(screen);
            SDL_FreeSurface(fond);
            break;

        case 4:
            fond= SDL_LoadBMP("Menu/Aides.bmp");
            SDL_BlitSurface(fond, NULL , screen, &posFond);
            SDL_Flip(screen);
            SDL_FreeSurface(fond);
            break;

        case 5:
            fond= SDL_LoadBMP("Menu/Scores.bmp");
            SDL_BlitSurface(fond, NULL , screen, &posFond);
            SDL_Flip(screen);
            SDL_FreeSurface(fond);
            break;

        case 6:
            fond= SDL_LoadBMP("Menu/Credit.bmp");
            SDL_BlitSurface(fond, NULL , screen, &posFond);
            SDL_Flip(screen);
            SDL_FreeSurface(fond);
            break;

        case 7:
            fond= SDL_LoadBMP("Menu/Quitter.bmp");
            SDL_BlitSurface(fond, NULL , screen, &posFond);
            SDL_Flip(screen);
            SDL_FreeSurface(fond);
            break;

        }
        pause=1;

        while (pause!=0)
        {

            SDL_WaitEvent(&event);

            switch(event.type)
            {
            case SDL_QUIT:
                run=0;
                break;
            case SDL_KEYDOWN:
                switch(event.key.keysym.sym)
                {
                case SDLK_UP:
                    switch(i)
                    {
                    case 1:
                        i=7;
                        break;
                    case 2:
                        i=1;
                        break;
                    case 3:
                        i=2;
                        break;
                    case 4:
                        i=3;
                        break;
                    case 5:
                        i=4;
                        break;
                    case 6:
                        i=5;
                        break;
                    case 7:
                        i=6;
                        break;

                    }
                    break;
                case SDLK_DOWN:
                    switch(i)
                    {
                    case 1:
                        i=2;
                        break;
                    case 2:
                        i=3;
                        break;
                    case 3:
                        i=4;
                        break;
                    case 4:
                        i=5;
                        break;
                    case 5:
                        i=6;
                        break;
                    case 6:
                        i=7;
                        break;
                    case 7:
                        i=1;
                        break;

                    }
                    break;
                case SDLK_RETURN:
                    switch(i)
                    {
                        case 1:
                            SCORE=Confirm_nouveau(screen);
                            SDL_FreeSurface(fond);
                            i=1;
                        break;
                    case 2:
                            //Continuer
                        break;
                    case 3:
                            //Options
                        break;
                    case 4:
                            Confirm_aide(screen);
                            i=4;
                        break;
                    case 5:
                            //Scores
                        break;
                    case 6:
                            //credits
                        break;
                    case 7:
                        run=0;
                        break;
                    }
                    break;
                }

                break;

            }
        pause=0;
        }
    }
    SDL_FreeSurface(fond);
    SDL_QUIT;
    return 0;
    }
